<?php

class M_mausac extends CI_Model {

// Constructor model
    function __construct() {
        parent::__construct();

        // Load database
        $this->load->database();
    }

    // Lấy dữ liệu từ database
    function getAllColor() {

        // Lấy tất cả dữ liệu trong bảng data
        $sql = $this->db->get('mausac');
        $result = $sql->result();

        // Trả kết quả về cho Controller
        return $result;
    }

    function getColor($MaMauSac) {
        $results = $this->db->get_where('mausac', array('MaMauSac' => $MaMauSac))->result();
        $result = $results[0];
        return $result;
    }

    function getMaMauSac($TenMauSac) {
        $item = 0;
        $query = $this->db->get_where('mausac', array('TenMauSac' => $TenMauSac));
        foreach ($query->result() as $row) {
            $item = $row->MaMauSac;
        }

        return $item;
    }

    function deleteMauSac($MaMauSac) {
        $this->db->delete('mausac', array('MaMauSac' => $MaMauSac));
    }

    function insertMauSac($data) {
        if ($this->db->insert('mausac', $data)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function updateMauSac($MaMauSac, $data) {
        $this->db->where('MaMauSac', $MaMauSac);
        $this->db->update('mausac', $data);
    }

    function getLastMauSac() {
        $results = $this->db->query('SELECT MaMauSac FROM mausac ORDER BY MaMauSac DESC LIMIT 1')->result();
        if ($results == null) {
            return 0;
        } else {
            $result = $results[0]->MaMauSac;
            return $result;
        }
    }

}
